package com.cloud.service.impl.pay;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cloud.constant.AuthConstants;
import com.cloud.constant.PayConstants;
import com.cloud.dao.pay.PayMapper;
import com.cloud.dao.user.UserMapper;
import com.cloud.exception.ValidateException;
import com.cloud.pojo.pay.CDKeyInfo;
import com.cloud.service.PayService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @version v1.0
 * @author: TianXiang
 * @description:
 * @date: 2021/2/24
 */
@Service
public class PayServiceImpl implements PayService {

    @Resource
    private PayMapper payMapper;

    @Resource
    private UserMapper userMapper;

    @Value("${business.cdkey.generator.num}")
    private Integer cdkeyNum;

    @Override
    public void checkCDKEY(String cdkey) {
        QueryWrapper query = new QueryWrapper();
        query.eq("cdkey", cdkey);
        query.eq("use_flag", PayConstants.CDKEY_NOT_USE);
        CDKeyInfo cdKeyInfo = payMapper.selectOne(query);
        if(null == cdKeyInfo) {
            throw new ValidateException("无效卡密!");
        }
        // 卡密置为已使用
        UpdateWrapper payUpdate = new UpdateWrapper();
        payUpdate.eq("cdkey", cdkey);
        payUpdate.set("use_flag", PayConstants.CDKEY_USE);
        payUpdate.set("use_time", new Date());
        payMapper.update(null, payUpdate);
        // 修改用户权限
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = ((UserDetails) principal).getUsername();
        UpdateWrapper userUpdate = new UpdateWrapper();
        userUpdate.eq("username", username);
        userUpdate.set("role", AuthConstants.VIP);
        userUpdate.set("expire_time", DateUtil.offsetDay(new Date(), 365));
        userMapper.update(null, userUpdate);
    }

    @Override
    public int generatorCDKEY() {
        List<CDKeyInfo> cdkeyList = Lists.newArrayList();
        for(int i = 0; i < this.cdkeyNum; i++) {
            CDKeyInfo cdKeyInfo = new CDKeyInfo()
                    .setCdkey(DigestUtils.md5DigestAsHex(UUID.randomUUID().toString().getBytes()).substring(0, 20))
                    .setUseFlag(PayConstants.CDKEY_NOT_USE);
            cdkeyList.add(cdKeyInfo);
        }
        return payMapper.insertBatch(cdkeyList);
    }

    @Override
    public String getCDKEY() {
        QueryWrapper query = new QueryWrapper();
        query.eq("use_flag", PayConstants.CDKEY_NOT_USE);
        return ((CDKeyInfo)payMapper.selectList(query).get(0)).getCdkey();
    }


}
